package lk.icta.common.dao.impl;

import lk.icta.common.dao.ContactsDAO;
import lk.icta.common.entity.Contacts;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.List;

/**
 * @author Supun Nimesh Karunathilaka
 */
public class ContactsDAOImpl extends BaseDAO implements ContactsDAO {

    @Transactional(propagation = Propagation.MANDATORY, readOnly = true)
    public void updateContacts(Contacts contact, User user) {
        em.merge(contact);
    }

    @Transactional(propagation = Propagation.MANDATORY, readOnly = true)
    public void deleteContact(Contacts contacts, User user) {
        em.remove(contacts);
        em.flush();
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void addContacts(Contacts contact, User user) {
        em.persist(contact);
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public Contacts getContactsByIdUKey(long id, User user) {
        logger.debug("get contact by contact id  {} ", id);
        return em.find(Contacts.class, id);
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public List<Contacts> findAll() {
        Query q = em.createNamedQuery("findAllContacts");
        return q.getResultList();
    }
}
